package com.xlx.ui.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xlx.ui.domain.DTO.UserDTO;
import com.xlx.ui.domain.MO.User;
import com.xlx.ui.domain.VO.ResultBody;
import com.xlx.ui.service.UserService;
import com.xlx.ui.service.error.errorImpl.BizException;
import com.xlx.ui.service.error.errorImpl.ResponseCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description:
 * @author: 张川
 * @date: 2020-11-30 21:29
 */

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    private UserService userService;



    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public ResultBody login(@RequestBody UserDTO userDTO) {
        ResultBody resultBody = userService.login(userDTO);
        return resultBody;
    }

    @ApiOperation(value = "注册")
    @PostMapping(value = "/register")
    public ResultBody register(@RequestBody UserDTO userDTO) {
        return userService.register(userDTO);
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("/update")
    public ResultBody updateUser(@RequestBody UserDTO userDTO) {
        return userService.update(userDTO) ? ResultBody.success() : ResultBody.error(ResponseCodeEnum.UPDATE_ERROR);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/deluser")
    public ResultBody deluser(String id) {
        return userService.del(id);
    }

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/all")
    public ResultBody getalluser() {
        return userService.getAll();
    }

    @ApiOperation(value = "获取单个用户信息")
    @GetMapping("/one")
    public ResultBody getUser(String email) {
        return ResultBody.success(userService.getUser(email));
    }

    @ApiOperation(value = "获取邮箱验证码")
    @GetMapping("/getemailcode")
    public ResultBody getemailcode(String email) {
        return userService.getEmailcode(email);
    }

    @ApiOperation(value = "更改头像")
    @PostMapping("/photo")
    public ResultBody photo(@RequestParam("picFile") MultipartFile picture,Integer userId) {
        String photoName = userService.photo(picture,userId);
        return ResultBody.success(photoName);
    }

    @ApiOperation(value = "搜索用户")
    @GetMapping("/search/{userId}/{query}")
    public ResultBody search(@PathVariable("userId")Integer userId,@PathVariable("query")String query){
        if(StrUtil.isBlank(query)){
            throw new BizException(ResponseCodeEnum.PARAMETER_INVALID);
        }
        List<User> search = userService.search(userId,query);
        return ResultBody.success(search);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/password")
    public ResultBody updataPassword(@RequestBody JSONObject params){
        Integer userid = params.getInteger("userid");
        String oldPassword = params.getString("oldPassword");
        String newPassword = params.getString("newPassword");
        if(userid==null||oldPassword==null||newPassword==null){
           throw new BizException(ResponseCodeEnum.PARAMETER_INVALID);
        }
        boolean b = userService.updatePassword(userid, oldPassword, newPassword);
        return b?ResultBody.success():ResultBody.error(ResponseCodeEnum.UPDATE_ERROR);
    }


}



